﻿using System;
using NExt.Handler.Base.Common.Cache;

namespace NExt.Handler.Base.Common.Manager
{
    public class InstanceManager : ConcurrentCache<InstanceManager, Type, object>
    {
        private static Func<Type, object> ObjectBuilder;

        public static void SetObjectBuilder(Func<Type, object> objectBuilder)
        {
            ObjectBuilder = objectBuilder;
        }

        public static object GetInstance(Type type)
        {
            object instance = GetOrAdd(type, () => ObjectBuilder(type));
            return instance;
        }

        public static object GetInstance(string fileName, string typeName)
        {
            Type type = TypeManager.GetType(fileName, typeName);
            object instance = GetInstance(type);
            return instance;
        }
    }
}
